<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>更改相似度 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/changing-similarities.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/changing-similarities.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/changing-similarities.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/changing-similarities.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="search-in-depth.html">深入搜索</a></span>
»
<span class="breadcrumb-link"><a href="controlling-relevance.html">控制相关度</a></span>
»
<span class="breadcrumb-node">更改相似度</span>
</div>
<div class="navheader">
<span class="prev">
<a href="pluggable-similarites.html">« 可插拔的相似度算法</a>
</span>
<span class="next">
<a href="relevance-conclusion.html">调试相关度是最后 10% 要做的事情 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="changing-similarities"></a>更改相似度<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/170_Relevance/75_Changing_similarities.asciidoc">edit</a>
</h2>
</div></div></div>
<p>相似度算法可以按字段指定，只需在映射中为不同字段选定即可：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">PUT /my_index
{
  "mappings": {
    "doc": {
      "properties": {
        "title": {
          "type":       "string",
          "similarity": "BM25" <a id="CO122-1"></a><i class="conum" data-value="1"></i>
        },
        "body": {
          "type":       "string",
          "similarity": "default" <a id="CO122-2"></a><i class="conum" data-value="2"></i>
        }
      }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO122-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">title</code> 字段使用 BM25 相似度算法。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO122-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">body</code> 字段用默认相似度算法（参见 <a class="xref" href="practical-scoring-function.html" title="Lucene 的实用评分函数">实用评分函数</a>）。</p>
</td>
</tr>
</table>
</div>
<p>目前，Elasticsearch 不支持更改已有字段的相似度算法 <code class="literal">similarity</code> 映射，只能通过为数据重新建立索引来达到目的。</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_配置_bm25"></a>配置 BM25<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/170_Relevance/75_Changing_similarities.asciidoc">edit</a>
</h3>
</div></div></div>
<p>配置相似度算法和配置分析器很相似，自定义相似度算法可以在创建索引时指定，例如：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">PUT /my_index
{
  "settings": {
    "similarity": {
      "my_bm25": { <a id="CO123-1"></a><i class="conum" data-value="1"></i>
        "type": "BM25",
        "b":    0 <a id="CO123-2"></a><i class="conum" data-value="2"></i>
      }
    }
  },
  "mappings": {
    "doc": {
      "properties": {
        "title": {
          "type":       "string",
          "similarity": "my_bm25" <a id="CO123-3"></a><i class="conum" data-value="3"></i>
        },
        "body": {
          "type":       "string",
          "similarity": "BM25" <a id="CO123-4"></a><i class="conum" data-value="4"></i>
        }
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO123-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>创建一个基于内置 <code class="literal">BM25</code> ，名为 <code class="literal">my_bm25</code> 的自定义相似度算法。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO123-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>禁用字段长度规范化（field-length normalization）。参见 <a class="xref" href="pluggable-similarites.html#bm25-tunability" title="BM25 调优">调试 BM25</a> 。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO123-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">title</code> 字段使用自定义相似度算法 <code class="literal">my_bm25</code> 。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO123-4"><i class="conum" data-value="4"></i></a></p>
</td>
<td align="left" valign="top">
<p>字段 <code class="literal">body</code> 使用内置相似度算法 <code class="literal">BM25</code> 。</p>
</td>
</tr>
</table>
</div>
<div class="tip admon">
<div class="icon"></div>
<div class="admon_content">
<p>自定义的相似度算法可以通过关闭索引，更新索引设置，开启索引这个过程进行更新。这样可以无须重建索引又能试验不同的相似度算法配置。</p>
</div>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="pluggable-similarites.html">« 可插拔的相似度算法</a>
</span>
<span class="next">
<a href="relevance-conclusion.html">调试相关度是最后 10% 要做的事情 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>